<?php 
	require_once("MoneyFactory.class.php"); 
	require_once("MoneyOps.class.php");
	require_once("bdd.class.php");
?>

<html>
	<head>
		<title>Porte Monnaie - itération 2</title>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
	</head>
	<body>
		<?php

			$bdd = new BDD();

			// On ajoute une valeur dans le porte monnaie si necessaire
			if (isset($_POST["delete"])) {
				$bdd->deleteAll();
			} else if (isset($_POST["add"])){
				if ( isset($_POST["v1"]) && isset($_POST["type"]) && isset($_POST["devise"]) ) {
					$moneyToAdd;

					// on cree la devise au cas ou elle n'existe pas en base (ne cree pas de doublons)
					$bdd->addCurrency($_POST['devise']);

					if ($_POST["type"] === "valueNote") {
						$moneyToAdd = MoneyFactory::getDefaultFactory()->createMoney($_POST["v1"], 0, $_POST["devise"]);
					} else if ($_POST["type"] === "valueMoney") {
						$moneyToAdd = MoneyFactory::getDefaultFactory()->createMoney(0, $_POST["v1"], $_POST["devise"]);
					}
					$bdd->addMoney($moneyToAdd);
				}
			} else if (isset($_POST["sup"])){
				if ( isset($_POST["v2"]) && isset($_POST["type"]) && isset($_POST["devise"]) ) {
					$moneyToSup;

					// TO DO !
				}
				
			}

			// On affichage le porte monnaie
			$array = array("EUR", "CHF", "USD");

			if(sizeof($array) > 0){
				echo "<h1>Votre Porte Monnaie</h1>";
			} else {
				echo "<h1>Il n'y a aucune devise dans la BDD</h1>";
			}
			echo "<br>";

			for($i = 0; $i<sizeof($array);$i++) {

				// on ajoute la currency si elle n'existe pas (ca ne cree pas de doublons dans la base)
				$bdd->addCurrency($array[$i]);
				$idCurrency = $bdd->getIDCurrencyFromName($array[$i]);

				$money = $bdd->getMoneyFromCurrencyId($idCurrency);

				echo $money->getValueNote()." ".$array[$i]." en billets - ";
				echo $money->getValuePiece()." ".$array[$i]." en pieces - ";
				echo  "Total : " . ($money->getValueNote() +  $money->getValuePiece()) . " " . $array[$i];
				echo "<br>";
			}

		?>
		<hr width='20%' align='left'/>
		<form action='index.php' method='post' id='form'>
		<table>
			<tr>
				<td colspan='5' align='left'>
					<h1>Ajouter de la monnaie</h1>
				</td>
			</tr>
			<tr>
				<td>Valeur</td>
				<td>Devise</td>
				<td>Type</td>
			</tr>
			<tr>
				<td><input type='text' id='v1' name='v1'/></td>
				<td>
					<select name="devise" id="devise">
						<option value="EUR">EUR</option>
						<option value="CHF">CHF</option>
						<option value="USD">USD</option>
					</select>
				</td>
				<td>
					<select name="type" id="type">
						<option value="valueNote">billets</option>
						<option value="valueMoney">pieces</option>
					</select>
				</td>
			  <td colspan='5' align='right'><input type='submit' value='Ajouter' id='add' name ='add'/></td>		
			</tr>
			<tr>
				<td colspan='5' align='left'>
					<hr/>
					<h1>Supprimer de la monnaie</h1>
				</td>
			</tr>
			<tr>
				<td>Valeur</td>
				<td>Devise</td>
				<td>Type</td>
			</tr>
			<tr>
				<td><input type='text' id='v2' name='v2'/></td>
				<td>
					<select name="devise" id="devise">
						<option value="EUR">EUR</option>
						<option value="CHF">CHF</option>
						<option value="USD">USD</option>
					</select>
				</td>
				<td>
					<select name="type" id="type">
						<option value="valueNote">billets</option>
						<option value="valueMoney">pieces</option>
					</select>
				</td>
			  <td colspan='5' align='right'><input type='submit' value='Supprimer' id='sup' name='sup'/></td>		
			</tr>
			<tr>
				<td colspan='5' align='left'>
					<input type='submit' id='delete' name='delete' value='Reinitialiser le porte monnaie'/>
				</td>
			</tr>
		</table>
		</form>
	</body>
</html>
